<template>
  <div class="app-container">
    <div
      v-show="showDialog && !detailInfo.visible && !assetDetailInfo.visible"
      class="app-container-flex"
    >
      <div class="width100">
        <!-- 条件栏 -->
        <div class="filter-wrapper">
          <NewPageFilter
            v-model:query="filterInfo.query.queryMap"
            :query-more-json="true"
            :filter-list="filterInfo.searchList"
            :clear-type="clearType"
            :slip-select-request="slipSelectRequest"
            :filter-btn="filterInfo.searchBtn"
          >
          </NewPageFilter>
          <el-button @click="closeDetailList">订单页</el-button>
        </div>
        <div class="filter-wrapper">
          <NewPageFilter :filter-btn="filterInfo.btnList">
            <template v-slot:filter-excel>
              <el-button :disabled="groupFlag" @click="exportExcel()"
                >导出excel
              </el-button>
            </template>
            <!-- <template v-slot:filter-print>
              <el-button :disabled="groupFlag" @click="print()"
                >打印
              </el-button>
            </template> -->
          </NewPageFilter>
          <div class="display-flex">
            <el-button @click="preferenceInfo.visible = true"
              >偏好设置</el-button
            >
          </div>
        </div>
        <table-container
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="tableInfo.api"
          :query="filterInfo.query"
          :check-box="false"
          :tab-index="true"
          :field-list="tableInfo.fieldList"
          @sortChange="sortChange"
          @handleEvent="handleEvent"
          @pageInfo="pageInfo"
        >
          <template v-slot:col-orderNo="scope">
            <span class="slip-edit" @click="showDetail(scope.row)">{{
              scope.row.orderNo
            }}</span>
          </template>
          <template v-slot:col-assetName="scope">
            <!-- class="slip-edit" @click="showAssetDetail(scope.row)" -->
            <span >{{
              scope.row.assetName
            }}</span>
          </template>
        </table-container>
      </div>
    </div>
    <set-preference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      @callback="setPreferenceCallback"
    />
    <advanced-search
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      :field-list="tableInfo.fieldList"
      v-model:form-data="advancedSearchInfo.data"
      @callback="advancedSearchCallback"
    />
    <order-detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :table-row="detailInfo.data"
      @returnBack="getList"
    />
    <asset-card-detail
      v-if="assetDetailInfo.visible"
      :id="assetDetailInfo.id"
      v-model:visible="assetDetailInfo.visible"
      :title="assetDetailInfo.title"
    />
    <page-dialog
      v-show="dialogInfo.visible"
      :title="dialogInfo.title"
      v-model:visible="dialogInfo.visible"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </page-dialog>
  </div>
</template>
<script setup name="patrolRecordRegistrationDetailList">
import {
  getPatrolRecoredCustomizeListAll,
  excelrecoredDetailOrderList,
  printRecoredOrderList,
} from '@/api/patrol.js'
import { fatchTemplateByCode } from '@/api/base'
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'
import PageDialog from '@/components/PageDialog/index'
import OrderDetail from './detail'
import AssetCardDetail from '@/views/asset/assetManage/assetCard/details'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps(['visible'])
const dialogInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: true,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '打印',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})

const filterInfo = ref({
  sortArr: [],
  query: {
    groupMap: {},
    groups: [],
    sortString: '',
    queryMap: {
      
    },
    customizeUserVO: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    // { type: 'button', label: '高级查询', event: advancedSearchShow },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      value: 'excel',
      type: 'slot',
      show: proxy.$permission('patrolRecordRegistration:excelDetailList'),
    },
    {
      type: 'slot',
      value: 'print',
      show: proxy.$permission('patrolRecordRegistration:printDetailList'),
    },
  ],
})
const clearType = ref(true)
const groupFlag = ref(false)
const slipSelectRequest = ref(true)

// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: undefined,
  data: [],
  selections: [],
  fieldList: [],
})

const preferenceInfo = ref({
  visible: false,
})
const detailInfo = ref({
  visible: false,
  data: {},
})
const advancedSearchInfo = ref({
  visible: false,
  data: {},
})
const assetDetailInfo = ref({
  visible: false,
  id: '',
  title: '资产详情',
})
const page = ref({
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

let obj = {
  assetName: {
    label: '设备名称',
    value: 'assetName',
    componentType: 'input',
    show: false,
    type: 'column',
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
getCustomize()
function pageInfo(obj) {
  page.value = obj
}
function closeDetailList() {
  showDialog.value = false
  emits('callback')
}

//导出excel
async function exportExcel() {
  dialogInfo.value.visible = true
  dialogInfo.value.type = 'excel'
  dialogInfo.value.msg = '请选择数据导出范围'
  dialogInfo.value.btList[1].label = '导出'
  dialogInfo.value.flag = true
}
function exportExcelList() {
  let params = {
    ...filterInfo.value.query,
    ...page.value,
    page: dialogInfo.value.flag,
  }
  excelrecoredDetailOrderList(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function handleClose() {
  dialogInfo.value.visible = false
}
function checkConfirm() {
  if (dialogInfo.value.type === 'excel') {
    exportExcelList()
  } else if (dialogInfo.value.type === 'print') {
    printList()
  }
  dialogInfo.value.visible = false
  dialogInfo.value.msg = ''
}
async function print() {
  //打印
  dialogInfo.value.visible = true
  dialogInfo.value.type = 'print'
  dialogInfo.value.msg = '请选择当数据打印范围'
  dialogInfo.value.btList[1].label = '打印'
  dialogInfo.value.flag = true
}
function printList() {
  fatchTemplateByCode({
    resourceCode: 'amortizationDeptAdjust:printDetailList',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp, dialogInfo.value.flag)
    }
  })
}
function getPrintData(temp, flag) {
  let query = {
    customizeDto: filterInfo.value.query,
  }
  let params = {
    ...query,
    ...page.value,
    page: flag,
  }
  printRecoredOrderList(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
function advancedSearchShow() {
  advancedSearchInfo.value.visible = true
}
async function advancedSearchCallback(obj) {
  filterInfo.value.query.queryMap = {}
  let arr = [
    // {
    //   fieldName: 'adjustType',
    //   queryType: 'EQUALS',
    //   value: 1,
    //   queryCategory: 'AND',
    // },
  ]
  filterInfo.value.query.queryDtoList = [...obj.queryDtoList, ...arr]
  advancedSearchInfo.value.data = obj.data

    await getList()
  
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
}
// 获取列表
async function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryDtoList = []

    tableInfo.value.initCurpage = Math.random()
    tableInfo.value.refresh = Math.random()
  
}
function showDetail(row) {
  detailInfo.value.data = row
  detailInfo.value.visible = true
}
function showAssetDetail(row) {
  assetDetailInfo.value.id = row.assetId
  assetDetailInfo.value.visible = true
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(filterInfo.value.copyQueryMap)
  search()
}
function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'patrolRecoredDetailList-com.slip.spd.entity.vo.patrol.PatrolRecoredDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      let tableFieldInfo = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = tableFieldInfo.fieldList
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.value.api = getPatrolRecoredCustomizeListAll
      getList()
    }
  })
}
function sortChange(column) {
  if (filterInfo.value.query.customizeUserVO.head.sortType === 0) {
    let arr = proxy.$fn.sortChange(column, filterInfo.value.sortArr)
    filterInfo.value.sortArr = arr
    filterInfo.value.query.sortString = arr.toString()
    getList()
  }
}
</script>

<style scoped></style>
